package com.rongzhe.house.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.ui.activity.HouseInfoActivity;
import com.rongzhe.house.utils.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by huang on 2017/12/2.
 */

public class HouseAdapter extends BaseAdapter {

    private Context mContext;
    private List<HouseOverview> mList;
    private LayoutInflater mLayoutInflater;
    private static String THUMBNAIL_SUFFIX = null;
    private static final String THUMBNAIL_SUFFIX_FORMAT = "imageView2/1/w/%s/h/%s";

    public HouseAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addHouseInfos(List<HouseOverview> houseInfos) {
        if (houseInfos == null || houseInfos.size() < 1) {
            return;
        }
        if (mList == null) {
            mList = new ArrayList<>();
        }
        mList.addAll(houseInfos);
        notifyDataSetChanged();
    }

    public void setHouseInfos(List<HouseOverview> houseInfos) {
        mList = houseInfos;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList == null ? null : mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.list_item_house_info, parent,
                    false);
        }
        MyViewHolder holder = (MyViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        }
        final HouseOverview houseOverview = mList.get(position);
        holder.textHouseTitle.setText(houseOverview.getName());
        holder.textRent.setText(houseOverview.getRent().toString());
        if (TextUtils.isEmpty(houseOverview.getType())) {
            holder.textHoustAcreageAndFloor.setText(houseOverview.getArea() + "㎡");
        } else {
            holder.textHoustAcreageAndFloor.setText(houseOverview.getType()
                    + " - " + houseOverview.getArea() + "㎡");
        }
        holder.textRent.setText(houseOverview.getRent().toString()+"元/月");
        if (houseOverview.getHouseTrafficList().size() > 0) {
            String traffic = houseOverview.getHouseTrafficList().get(0).getTraffic();
            holder.textLocation.setText(traffic);
        } else {
            holder.textLocation.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(houseOverview.getCover())) {
            ImageView housePhoto = holder.imageHousePhoto;
            if (TextUtils.isEmpty(THUMBNAIL_SUFFIX)) {
                THUMBNAIL_SUFFIX = String.format(THUMBNAIL_SUFFIX_FORMAT,
                        String.valueOf(UIUtils.dip2px(mContext, 130)),
                        String.valueOf(UIUtils.dip2px(mContext, 90)));
            }
            Picasso.with(mContext).load(houseOverview.getCover() + "?" + THUMBNAIL_SUFFIX)
                    .into(housePhoto);
        }

        holder.containerHouseLable.removeAllViews();
        String textRentType;
        if (houseOverview.getEntireRent() != null && houseOverview.getEntireRent()) {
            textRentType = mContext.getString(R.string.rent_all);
        } else {
            textRentType = mContext.getString(R.string.rent_together);
        }
        TextView lable = (TextView) mLayoutInflater.inflate(R.layout.item_lable_text,
                holder.containerHouseLable, false);
        lable.setText(textRentType);
        holder.containerHouseLable.addView(lable, 0);

        if (houseOverview.getOwnerRent() != null && houseOverview.getOwnerRent()) {
            TextView lable2 = (TextView) mLayoutInflater.inflate(R.layout.item_lable_text,
                    holder.containerHouseLable, false);
            lable2.setText(R.string.rent_direct);
            holder.containerHouseLable.addView(lable2, 0);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, HouseInfoActivity.class);
                intent.putExtra(HouseInfoActivity.EXTRA_HOUSE_ID, houseOverview.getId());
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    /**
     * 正常布局的ViewHolder
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_house_photo)
        ImageView imageHousePhoto;
        @BindView(R.id.text_house_title)
        TextView textHouseTitle;
        @BindView(R.id.text_houst_acreage_and_floor)
        TextView textHoustAcreageAndFloor;
        @BindView(R.id.text_location)
        TextView textLocation;
        @BindView(R.id.container_house_lable)
        LinearLayout containerHouseLable;
        @BindView(R.id.text_rent)
        TextView textRent;
        View root;

        public MyViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
}
